﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.Doc;
using UFIDA.U9.CBO.SCM.Enums;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFIDA.U9.PM.ASN;
using UFIDA.U9.PM.PO;
using UFIDA.U9.SM.RMR;
using UFIDA.U9.SM.ShipPlan;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.RMRP
{
    public class AfterUpdating : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.SM.RMR.RMRHead;
            if (holder == null)
                return;
            #endregion
            //审核
            if (holder.Status == RMRStatusEnum.Approved && holder.OriginalData.Status == RMRStatusEnum.Approving)
            {


                HttpHelper httpHelper = new HttpHelper();
                JsonConfig config = new JsonConfig();
                httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/create-warehouse-receipt";



                WarehouseReceiptCreateOpenV3Req dto = new WarehouseReceiptCreateOpenV3Req();

                dto.tenantId = "1";
                dto.sourceSysType = "ERP";
                dto.billGroupCode = "XSR01";
                dto.billTypeCode = "XSR01-01";

                dto.billSourceCode = holder.DocumentType.Code;
                dto.billSourceText = holder.DocumentType.Name;
                dto.billSourceNumber = holder.DocNo;
                dto.urgencyDegree = "NORMAL";
                dto.sourceType = "TARGET_CUSTOMER";
                dto.sourceCode = holder.Customer.Customer.Code;
                dto.plannedTime = holder.BusinessDate.ToString();
                dto.completionTime = holder.BusinessDate.ToString();
                dto.remark = holder.Remark;

                List<WarehouseRequirementCreateOpenV3Req> dtolines = new List<WarehouseRequirementCreateOpenV3Req>();

                foreach (var line in holder.RMRs)
                {



                    WarehouseRequirementCreateOpenV3Req dtoit = new WarehouseRequirementCreateOpenV3Req();
                    dtoit.materialCode = line.ItemInfo.ItemCode;
                    dtoit.customerCode = holder.Customer.Customer.Code;
                    dtoit.batchNo = line.Lot?.LotCode ?? "";
                    dtoit.warehouseCode = line.ItemInfo.ItemID.InventoryInfo.Warehouse?.Code ?? "";
                    dtoit.productionDate = holder.BusinessDate.ToString();
                    dtoit.entryTime = holder.BusinessDate.ToString();
                    dtoit.qualityLevel = "QUALIFIED_PRODUCT";
                    dtoit.lineNumber = line.RMRNo.ToString();
                    dtoit.plannedQuantity = line.ApplyQtyTU1;
                    //dtoit.barcodeQuantity = line.ApplyQtyTU1;
                    dtoit.sourceDetailId = line.ID;
                    dtoit.sourceBillCode = holder.DocNo;
                    dtoit.sourceDetailLineNumber = line.RMRNo.ToString();
                    dtolines.Add(dtoit);
                }

                dto.requirementList = dtolines;

                httpHelper.Content = JsonConvert.SerializeObject(dto);
                var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                if (result["code"].ToString() != "000000")
                {
                    throw new Exception(result["msg"].ToString());
                }
            }

            //弃审
            if (holder.Status == RMRStatusEnum.Opening && holder.OriginalData.Status == RMRStatusEnum.Approved)
            {
                HttpHelper httpHelper = new HttpHelper();
                JsonConfig config = new JsonConfig();
                httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/delete-warehouse-receipt";


                WMSDeleteDTO dto = new WMSDeleteDTO();
                dto.billSourceNumber = holder.DocNo;

                httpHelper.Content = JsonConvert.SerializeObject(dto);
                var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                if (result["code"].ToString() != "000000")
                {
                    throw new Exception(result["msg"].ToString());
                }
            }
        }
    }
}
